// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出异步调用 ============================== //

export default {
  // —————————————————————————————— 信息 —————————————————————————————— //

  async remoteInfo({ state: { API, info, param, DICT } }) {
    const [type, name, code] = DICT.TYPE.find((item) => item[0] === param.type)
    const typeList = DICT.TYPE.map(([id]) => id)
    const TIP = API.transTip(`查询${name}报表`)

    if (type === 5) {
      const [, arr] = await API.getList(param, TIP)
      CORE.merge(info[code], transAttendance(arr))
    } else {
      const obj = await API.getInfo(param, TIP)
      CORE.merge(info[code], transInfo(obj, type, typeList))
    }
  },

  // —————————————————————————————— 下载 —————————————————————————————— //

  async remoteDownload({ state: { API, downParam } }) {
    const TIP = API.transTip(`导出${downParam.sheetName}报表`)
    const msg = await API.download(downParam, TIP)
    return msg
  },
}

// ============================== 转换方法 ============================== //

const transAttendance = (arr) => {
  const table = arr.map((item, i) => {
    let rate = 0
    if (item.attendance) rate = +((item.attendanceReal || 0) * 100 / item.attendance).toFixed(2)

    return {
      ...item,
      id: i + 1,
      attendanceRate: `${rate}%`,
    }
  })
  return { table }
}

const transInfo = (obj, type, typeList) => {
  if (type === typeList[0]) {
    const all = getPieData(obj.taskMap)
    const complete = getPieData(obj.finishTaskMap)
    const table = getTableData(obj.reportFormRepairDtos)
    const person = getRepairPersonData(obj.finishTaskSubMap)
    return { all, complete, person, table }
  }

  if (type === typeList[1] || type === typeList[2]) {
    const all = getPieData(obj.taskMap)
    const complete = getBarData(obj.finishTaskMap, ['taskTotal', 'unfinishTask', 'ontimeFinishTask'])
    const table = getTableData(obj.reportFormInspectDtos || obj.reportFormMaintenanceDtos, true)
    const person = getTaskPersonData(obj.taskSubMap)
    return { all, complete, person, table }
  }

  if (type === typeList[3]) {
    const all = getPieData(obj.warnMap)
    const complete = getBarData(obj.finishWarnMap, ['averageDealTime', 'maxDealTime', 'minDealTime'])
    const table = getTableData(obj.reportFormWarnDtos)
    return { all, complete, table }
  }
}

// ============================== 其他方法 ============================== //

const getPieData = (map) => {
  const obj = CORE.isEmpty(map) ? {} : map
  const xData = Object.keys(obj)
  const dataList = xData.map((x) => obj[x])
  return { xData, dataList }
}

const getBarData = (map, keyList) => {
  const obj = CORE.isEmpty(map) ? { 无: {} } : map
  const xData = Object.keys(obj)
  const arrayList = keyList.map((key) => xData.map((x) => obj[x][key]))
  return { xData, arrayList }
}

const getTableData = (list, isPlan) => {
  let table = list || []
  if (isPlan) {
    table = (list || []).reduce((table, { name, inspectDtos, maintenanceDtos }) => {
      const array = inspectDtos || maintenanceDtos || []
      table.push(...array.map((row) => ({ ...row, name })))
      return table
    }, [])
  }
  return table.map((item, i) => ({ id: i + 1, ...item }))
}

const getRepairPersonData = (map) => {
  const obj = CORE.isEmpty(map) ? {} : map
  const person = {}

  const names = Object.keys(obj)
  names.forEach((name) => {
    const matched = obj[name] || {}
    person[name] = {
      xData: ['0-1小时', '1-3小时', '3小时以上'],
      dataList: [matched.hourLevel1 || 0, matched.hourLevel2 || 0, matched.hourLevel3 || 0],
      title: `${name}完成情况`,
    }
  })
  return person
}

const getTaskPersonData = (map) => {
  const obj = CORE.isEmpty(map) ? {} : map
  const person = {}

  const nameTasks = Object.keys(obj)
  nameTasks.forEach((nameTask) => {
    const matched = obj[nameTask] || {}
    const [name] = (nameTask || '').split(',')
    if (person[name]) {
      person[name].dataList[0] += matched.ontimeFinishTask || 0
      person[name].dataList[1] += matched.unfinishTask || 0
    } else {
      person[name] = {
        xData: ['按时完成', '未完成'],
        dataList: [matched.ontimeFinishTask || 0, matched.unfinishTask || 0],
        title: `${name}任务总数`,
      }
    }
  })
  return person
}
